﻿#region 文件信息

// FileName: SoCafeShop/Sohobloo.SoAuth.Web/RoleUserController.cs
// Author: Sohobloo
// CreateTime: 2013/07/02 20:12
// UpdateTime: 2013/07/05 0:51

#endregion

#region 引用

using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web.ApplicationServices;
using System.Web.Mvc;
using Sohobloo.SoAuth.Entity;
using Sohobloo.SoAuth.Service;
using Sohobloo.SoAuth.Web.Models;

#endregion

namespace Sohobloo.SoAuth.Web.Controllers
{
    [Export]
    public class PermissionController : Controller
    {
        [Import]
        public IPermissionService PermissionService { get; set; }

        [Import]
        public IRoleService RoleService { get; set; }

        [Import]
        public IFunctionService FunctionService { get; set; }

        [Import]
        public IActionService ActionService { get; set; }

        public ActionResult Index(Guid? id)
        {
            var permissionModel = new PermissionModel()
            {
                RoleId = id
            };

            var roleList = RoleService.GetRoleList();
            permissionModel.RoleList = roleList.Select(
                r => new RoleModel
                {
                    Id = r.Id,
                    RoleName = r.RoleName
                });
                var functionList = FunctionService.GetFunctionList();

                permissionModel.RoleFunctionList = functionList.Select(
                    u => new FunctionModel
                    {
                        Id = u.Id,
                        FunctionName = u.FunctionName
                    });

            if(id.HasValue)
            {
                var permissionList = PermissionService.GetRolePermissionList(id.Value);
                var actionModelList = permissionList.Select(
                    a => new ActionModel
                    {
                        Id = a.PermissionAction.Id,
                        ActionName = a.PermissionAction.ActionName,
                        FunctionName = a.PermissionFunction.FunctionName
                    });

                permissionModel.RoleActionList = actionModelList;
            }

            return View(permissionModel);
        }

        [HttpPost]
        public ActionResult GetPermissions(Guid roleId)
        {
            var role = RoleService.GetRole(roleId);
            var actionListModel = role.RolePermissions.Select(
                p => new ActionModel
                {
                    Id = p.PermissionAction.Id,
                    ActionName = p.PermissionAction.ActionName,
                    FunctionName = p.PermissionFunction.FunctionName
                });

            return PartialView("_PermissionList", actionListModel);
        }

        [HttpPost]
        public ActionResult GetActionList(Guid roleId, Guid functionId)
        {
            var actions = ActionService.GetActionList(functionId);
            var roleActions = PermissionService.GetRolePermissionList(roleId).Select(p => p.PermissionAction);
            var selectableActions = actions.Except(roleActions).OrderBy(a => a.CreateTime);
            var actionModelList = selectableActions.Select(
                a => new ActionModel
                {
                    Id = a.Id,
                    ActionName = a.ActionName
                });
            return PartialView("_ActionList", actionModelList);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(Guid roleId, Guid actionId)
        {
            PermissionService.CreatePermission(roleId, actionId);
            return Json(null);
        }

        public ActionResult Remove(Guid? id, Guid userId)
        {
            RoleService.RemoveUser(id.Value, userId);

            return RedirectToAction(
                "Index",
                new
                {
                    id
                });
        }

        [HttpPost]
        public ActionResult Remove(Guid id)
        {
            throw new NotImplementedException();
        }
    }
}